package controllers

import (
	"errors"
	"mdblog/models/auth"
)

var (
	PasswordNotInconsistentError = errors.New("两次密码不一致")
)

type UserQueryPaginator struct {
	Limit int
	Page  int
}

func (request UserQueryPaginator) Do() ([]auth.User, int64, error) {
	return auth.GetUsersPaginator(request.Limit, request.Page)
}

type UserCreateRequest struct {
	Username string `json:"username"`
	Password string `json:"password"`
	Email    string `json:"email"`
	Role     string `json:"role"`
}

func (request UserCreateRequest) Do() error {
	var err error
	if request.Role == "admin" {
		err = auth.CreateAdminUserAccount(request.Username, request.Password, request.Email)
	} else {
		err = auth.CreateUserAccount(request.Username, request.Password, request.Email)
	}
	return err
}

type UserChangePassRequest struct {
	Username  string `json:"username"`
	Password  string `json:"password"`
	Password2 string `json:"password2"`
}

func (request UserChangePassRequest) Do() error {
	if request.Password != request.Password2 {
		return PasswordNotInconsistentError
	}
	return auth.ChangePassword(request.Username, request.Password)
}
